const EcommerceCrawler = require('./ecommerce-crawler');
const config = require('./config');

async function testCrawler() {
    console.log('=== 电商爬虫测试 ===');
    
    const crawler = new EcommerceCrawler();
    
    try {
        // 初始化
        await crawler.init();
        
        // 测试打开淘宝主页
        console.log('\n1. 测试打开淘宝主页...');
        const taobaoSuccess = await crawler.openHomePage(config.websites.taobao.url);
        
        if (taobaoSuccess) {
            console.log('✅ 淘宝主页打开成功！');
            
            // 等待一下让用户看到页面
            await crawler.page.waitForTimeout(5000);
            
            // 关闭当前页面
            await crawler.page.close();
            
            // 创建新页面测试京东
            crawler.page = await crawler.browser.newPage();
            await crawler.page.setExtraHTTPHeaders({
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            });
            
            console.log('\n2. 测试打开京东主页...');
            const jdSuccess = await crawler.openHomePage(config.websites.jd.url);
            
            if (jdSuccess) {
                console.log('✅ 京东主页打开成功！');
                await crawler.page.waitForTimeout(5000);
            } else {
                console.log('❌ 京东主页打开失败！');
            }
        } else {
            console.log('❌ 淘宝主页打开失败！');
        }
        
    } catch (error) {
        console.error('测试过程中出错:', error);
    } finally {
        await crawler.close();
        console.log('\n=== 测试完成 ===');
    }
}

// 运行测试
testCrawler().catch(console.error);
